//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_playerKilled.sqf"
#include <btc_macros.h>
TRACE_1("Start",_this);


private ["_type_backpack","_weapons","_magazines","_weapon_backpack","_ammo_backpack","_score","_score_array","_name","_body_marker","_ui","_time_left","_idx"];
titleText ["", "BLACK OUT"];
_body = _this select 0;
[_body] spawn
{
	_body = _this select 0;
	_dir = getDir _body;
	_pos = getPosATL vehicle _body;
	if (BTC_lifes != 0 || BTC_active_lifes == 0) then
	{
		BTCR_NextGearSave = 0;
		WaitUntil {Alive player};
		detach player;
		//player setPos [getMarkerPos BTC_respawn_marker select 0, getMarkerPos BTC_respawn_marker select 1, 5000];
		_body_marker = player;
		player setcaptive true;
		BTC_r_camera_nvg = false;
		BTCR_pveh_killed = [2,_body_marker];publicVariable "BTCR_pveh_killed";player allowDamage false;
		player setVariable ["BTC_need_revive",1,true];
		player switchMove "AinjPpneMstpSnonWrflDnon";
		_actions = [] spawn BTCR_fnc_assignActions;
		if (BTC_respawn_gear == 1) then 
		{
			_gear = [player,BTC_gear] spawn BTCR_fnc_setGear;
		};
		WaitUntil {animationstate player == "ainjppnemstpsnonwrfldnon"};
		sleep 2;
		player setDir _dir;
		player setVelocity [0,0,0];
		player setPosATL _pos;
		if (BTC_gear_on_death == 1) then {
			TRACE("Save Gear");
			BTC_gear = [_body] call FUNC(getGear);
		};
		deletevehicle _body;
		_side = playerSide;
		_injured = player;
		if (BTC_injured_marker == 1) then {BTCR_pveh_marker = [0,BTC_side,_pos,_body_marker];publicVariable "BTCR_pveh_marker";};
		//TYRGHEN
		//disableUserInput true;
		for [{_n = BTC_revive_time_min}, {_n > 0 && player getVariable "BTC_need_revive" == 1}, {_n = _n - 0.5}] do
		{
			if (BTC_active_lifes == 1) then {titleText [format ["Lifes remaining: %1",BTC_lifes], "BLACK FADED"];} else {titleText ["", "BLACK FADED"];};
			sleep 0.5;
		};
		if (BTC_black_screen == 0) then {titleText ["", "BLACK IN"];};
		// TYRGHEN
		//disableUserInput false;
		_time = time;
		_timeout = _time + BTC_revive_time_max;
		private ["_id","_lifes"];
		BTC_respawn_cond = false;
		_ui = objNull;
		if (BTC_disable_respawn == 1) then {player enableSimulation false;};
		_id = player addAction [RED_TEXT("Respawn"),{(_this select 3) spawn BTCR_fnc_playerRespawn},[], 9, true, true, "", "true"];
		if (BTC_camera_unc == 0) then
		{
			if (BTC_black_screen == 0 && BTC_disable_respawn == 0) then {if (BTC_action_respawn == 0) then {disableSerialization;_dlg = createDialog "BTC_respawn_button_dialog";};};
			if (BTC_black_screen == 1 && BTC_disable_respawn == 0 && !Dialog) then {_dlg = createDialog "BTC_respawn_button_dialog";};
			BTC_display_EH = (findDisplay 46) displayAddEventHandler ["KeyDown", "_anim = [] spawn {sleep 1;player switchMove ""AinjPpneMstpSnonWrflDnon"";};"];
		}
		else
		{
			BTC_r_s_list   = [];
			BTC_r_s_target = player;
			BTC_r_u_camera = "camera" camCreate (position player);
			BTC_r_u_camera camSetTarget player;
			BTC_r_u_camera cameraEffect ["internal", "BACK"];
			BTC_r_u_camera camSetPos [(getpos player select 0) + 15,(getpos player select 1) + 15,10];
			BTC_r_u_camera camCommit 0;
			disableSerialization;
			_r_dlg = createDialog "BTC_spectating_dialog";
			_ui = uiNamespace getVariable "BTC_r_spectating";
			// TYRGHEN - allow changing targets
			if (BTC_spectating == 0) then { (_ui displayCtrl 120) ctrlShow false; } else { call BTCR_fnc_spectatingLoad};
			BTC_r_camera_EH_keydown = (findDisplay 46) displayAddEventHandler ["KeyDown", "_keydown = _this spawn BTCR_fnc_spectatingKeyDown"];
			{_lb = lbAdd [121,_x];if (_x == BTC_camera_unc_type select 0) then {lbSetCurSel [121,_lb];}} foreach BTC_camera_unc_type;
		};
		while {format ["%1", player getVariable "BTC_need_revive"] == "1" && time < _timeout && !BTC_respawn_cond} do
		{
			if (BTC_disable_respawn == 0) then {
				if (BTC_black_screen == 1 || (BTC_black_screen == 0 && BTC_action_respawn == 0)) then {
					if (!Dialog && !BTC_respawn_cond) then {
						_dlg = createDialog "BTC_respawn_button_dialog";
					};
				};
			};
			_healer = call BTCR_fnc_checkHealer;
			_lifes = "";
			if (BTC_active_lifes == 1) then {_lifes = format ["Lifes remaining: %1",BTC_lifes];};
			if (BTC_black_screen == 1 && BTC_camera_unc == 0) then {titleText [format ["%1\n%2\n%3", round (_timeout - time),_healer,_lifes], "BLACK FADED"]} else {hintSilent format ["%1\n%2\n%3", round (_timeout - time),_healer,_lifes];};
			if (BTC_camera_unc == 1) then 
			{
				// TYRGHEN - Added ability to spectate over others
				if (!dialog) then {
					sleep 1;
					disableSerialization;
					_r_dlg = createDialog "BTC_spectating_dialog";
					sleep 0.01;
					_ui = uiNamespace getVariable "BTC_r_spectating";
					BTC_r_s_list = [];
					if (BTC_spectating == 0) then {
						(_ui displayCtrl 120) ctrlShow false;
						BTC_r_s_target = player;
					} else {
						[] call BTCR_fnc_spectatingLoad;
					};
					{
						_lb = lbAdd [121,_x];
						if (_x == BTC_camera_unc_type select 0) then {
							lbSetCurSel [121,_lb];
						}
					} foreach BTC_camera_unc_type;
				} else {
					if (BTC_spectating == 0) then {
						[] call BTCR_fnc_spectatingLoad;
					};
				};
				_time_left = round (_timeout - time);
				ctrlSetText [123, format ["Time left: %1s", _time_left]];
				if (_time_left < 30) then {
					(_ui displayCtrl 123)  ctrlSetTextColor [1, 0, 0, 1];
				};
				ctrlSetText [124, _healer];
				BTC_r_u_camera camSetTarget BTC_r_s_target;
				BTC_r_u_camera attachTo [BTC_r_s_target,BTC_r_s_cam_view];
				BTC_r_u_camera camCommit 0;
				if (BTC_r_camera_nvg) then {camusenvg true;} else {camusenvg false;};				
			};
		};
		if (BTC_camera_unc == 0) then
		{
			(findDisplay 46) displayRemoveEventHandler ["KeyDown",BTC_display_EH];
		}
		else
		{
			player cameraEffect ["TERMINATE", "BACK"];
			camDestroy BTC_r_u_camera;	
			BTC_r_u_camera = objNull;
			(findDisplay 46) displayRemoveEventHandler ["KeyDown",BTC_r_camera_EH_keydown];
		};
		closedialog 0;
		// Fixed through the Function Container
		// player removeAllEventHandlers "handleDamage";
		// _idx = 0;
		// if (BTC_r_new_system == 0) then  {
			// _idx = player addEventHandler ["HandleDamage", BTCR_fnc_handleDamageGear];
		// } else {
			// _idx = player addEventHandler ["HandleDamage", BTCR_fnc_handleDamage];
		// };
		// TRACE_1("Handler Damage EH",_idx);
			
		if (time > _timeout && format ["%1", player getVariable "BTC_need_revive"] == "1") then 
		{
			_respawn = [] spawn BTCR_fnc_playerRespawn;
		};
		if (format ["%1", player getVariable "BTC_need_revive"] == "0" && !BTC_respawn_cond) then 
		{
			if (BTC_black_screen == 1) then {titleText ["", "BLACK IN"];} else {hintSilent "";};
			// TYRGHEN - Removed during the firstAid script
			//if (BTC_need_first_aid == 1 && ((items player) find "FirstAidKit" != -1)) then {player removeItem "FirstAidKit";};
			player playMove "amovppnemstpsraswrfldnon";
			player playMove "";
		};
		player removeAction _id;
		player setcaptive false;
		if (BTC_disable_respawn == 1) then {player enableSimulation true;};
		//player switchMove "";
		player allowDamage true;
		hintSilent "";
	};
};
TRACE("End");